/*
 * Copyright (c) 2018. net All Rights Reserved.
 * 项目名称：paascloud快速搭建企业级分布式微服务平台
 * 类名称：PtcAlipayServiceImpl.java
 * 创建人：刘兆明
 * 联系方式：net@gmail.com
 * 开源地址: https://github.com/paascloud
 * 博客地址: http://blog.net
 * 项目官网: http://net
 */

package com.chii.order.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;

import com.chii.order.mapper.OrderMapper;
import com.chii.order.po.Order;
import com.chii.order.service.OrderService;
import com.chii.order.service.PtcAlipayService;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * The class Ptc alipay service.
 *
 * @author net @gmail.com
 */
@Service
public class PtcAlipayServiceImpl implements PtcAlipayService {
	private Logger logger = LogManager.getLogger(PtcAlipayServiceImpl.class);

	@Value("${alipay.server_url}")
	private String server_url;
	@Value("${alipay.appid}")
	private String appid;
	@Value("${alipay.app_private_key}")
	private String app_private_key;
	@Value("${alipay.alipay_public_key}")
	private String alipay_public_key;
	@Value("${alipay.notify_url}")
	private String notify_url;
	@Value("${alipay.return_url}")
	private String return_url;
	@Autowired
	private OrderMapper orderMapper;

	@Override
	public void aliPayCallback(String orderNo) {
		logger.info("支付宝回调. - aliPayCallback.orderNo={}", orderNo);
		Order order = new Order();
		order.setOrderNo(orderNo);
		order.setPaymentState(true);
		order.setPaymentAt(new Date());
		order.setPaymentId(1);
		orderMapper.updateByOrderNo(order);
	}

	@Override
	public String pay(String orderNo,String orderDescription) {
		// 这个东西必备
		AlipayClient alipayClient = new DefaultAlipayClient(server_url,
				appid, app_private_key, "json", "UTF-8",
				alipay_public_key, "RSA2");
		// 发起App支付请求
		AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		// 订单描述
		model.setBody(orderDescription);
		// 订单标题
		model.setSubject(orderDescription);
		// 商户订单号 就是商户后台生成的订单号
		model.setOutTradeNo(orderNo);
		// 该笔订单允许的最晚付款时间，逾期将关闭交易。取值范围：1m～15d。m-分钟，h-小时，d-天，1c-当天 (屁股后面的字母一定要带，不然报错)
		model.setTimeoutExpress("24h");
		// 订单总金额 ，默认单位为元，精确到小数点后两位，取值范围[0.01,100000000]
		model.setTotalAmount("10");
		// 销售产品码 不必填
		model.setProductCode("QUICK_MSECURITY_PAY");
		request.setBizModel(model);
		request.setReturnUrl(return_url);
		request.setNotifyUrl(notify_url);
		try {
			// 通过api的方法请求阿里接口获得反馈
			AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
			System.out.println(response.getBody());
			if (response.isSuccess()) {
				System.out.println("调用成功");
			} else {
				System.out.println("调用失败");
			}
			return response.getBody();
		} catch (Exception e) {
			return e.getMessage();
		}
	}

}
